/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cas.struts.action;

import com.cas.dbaccess.CASDBAccessfactory;
import com.cas.hibernate.entity.Doctor;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorActionForm;

/**
 *
 * @author suraj
 */
public class DoctorAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "viewDoctors";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
         
        String forward = "viewDoctors";
        if(isCancelled(request))
        {
            return mapping.findForward(forward);
        }
        
        DynaValidatorActionForm dForm = (DynaValidatorActionForm)form;
        String actionType = dForm.getString("entityAction");
        System.out.println("DoctorAction........" + actionType);
        if("edit".equalsIgnoreCase(actionType))
        {
            forward = editDoctor(dForm);
        }else if("add".equalsIgnoreCase(actionType))
        {
            forward = addDoctor(dForm);
        }
        
        return mapping.findForward(forward);
    }
    
    private String editDoctor(DynaValidatorActionForm form)
    {
        String doctorId = form.getString("id");
        Doctor d = CASDBAccessfactory.getCASDbAccess().getDoctor(doctorId);
        d.setFirstName(form.getString("firstName"));
        d.setSecondName(form.getString("secondName"));  
        d.setFamilyName(form.getString("familyName"));
        d.setSpeciality(form.getString("speciality"));
        d.setSex(form.getString("sex").equals("1") ? 1 : 2);
        CASDBAccessfactory.getCASDbAccess().saveDoctor(d);
        return "viewDoctors";   
    }

    private String addDoctor(DynaValidatorActionForm form)
    {
        Doctor d = new Doctor();
        d.setFirstName(form.getString("firstName"));
        d.setSecondName(form.getString("secondName"));  
        d.setFamilyName(form.getString("familyName"));
        d.setSpeciality(form.getString("speciality"));
        d.setSex(form.getString("sex").equals("1") ? 1 : 2);
        CASDBAccessfactory.getCASDbAccess().createDoctor(d);
        return "viewDoctors";   
    }
       
}
